package com.atguigu.gmall.product;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class CompletableFutureDemo02 {
    public static void main(String[] args) {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                5, //核心线程
                10, //最大线程
                5, //闲置线程的存货时间
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(10),
                //工厂: 默认工厂
                //拒绝策略
                new ThreadPoolExecutor.CallerRunsPolicy()
        );

        //1.
        /*CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(2);
                System.out.println("我是上一个任务");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "上任务返回值";
        },threadPoolExecutor).thenApplyAsync((t) -> {
            System.out.println("thenApplyAsync:t:" + t);
            return "当任务返回值";
        },threadPoolExecutor).whenCompleteAsync((t,u) -> {
            System.out.println("whenCompleteAsync:t:" + t);
        });*/

        /*CompletableFuture.supplyAsync(() -> {
            System.out.println("我是上一个任务");
            return "上任务返回值";
        },threadPoolExecutor).thenAcceptAsync((t) -> {
            System.out.println("thenApplyAsync:t:" + t);
        },threadPoolExecutor).whenCompleteAsync((t,u) -> {
            System.out.println("whenCompleteAsync:t:" + t);
        });*/

        CompletableFuture.supplyAsync(() -> {
            System.out.println("我是上一个任务");
            return "上任务返回值";
        },threadPoolExecutor).thenRunAsync(() -> {
            System.out.println("我是当前任务:t:");
        },threadPoolExecutor).whenCompleteAsync((t,u) -> {
            System.out.println("whenCompleteAsync:t:" + t);
        });
    }
}
